package com.hsj.springai.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Collectors;

public class FutureTaskDemo {

}

class myCache {
    private static final Logger log = LoggerFactory.getLogger(myCache.class);
    private volatile Map<String, Object> map = new HashMap<>();
    private ReadWriteLock lock = new ReentrantReadWriteLock();

    public void put(String key, Object value) {
        //添加写锁
        lock.writeLock().lock();
        try {
            System.out.println(Thread.currentThread().getName() + "正在 写操作" + key);
            TimeUnit.SECONDS.sleep(1);
            map.put(key, value);
            System.out.println(Thread.currentThread().getName() + "已经写完了");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.writeLock().unlock();
        }
    }

    public Object get(String key) {
        lock.readLock().lock();
        try {
            Object result = null;
            System.out.println(Thread.currentThread().getName() + "正在 读取数据" + key);
            TimeUnit.SECONDS.sleep(1);
            result = map.get(key);
            System.out.println(Thread.currentThread().getName() + "已经取出来了,值" + result);
            return result;
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.readLock().unlock();
        }
    }

    public static void main(String[] args) {
        String  a="abcd,1234";
        String  a1="abcd,vnvn,5678";
        String  a2 = "qwed,123";
        String[] split = a.split(",");
        String[] split1 = a1.split(",");
        String[] split2 = a2.split(",");

        Set<String> collect = Arrays.stream(split).collect(Collectors.toSet());
        Set<String> collect1 = Arrays.stream(split1).collect(Collectors.toSet());
        Set<String> collect2 = Arrays.stream(split2).collect(Collectors.toSet());

        collect.retainAll(collect1);
        System.out.println(collect);

        collect2.retainAll(collect1);
        System.out.println(collect2);

    }

}
